import router from './router'
import store from '@/store'
import defaultSettings from '@/settings'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

const { homeName } = defaultSettings

NProgress.configure({ showSpinner: false }) // NProgress Configuration

router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()
  const LOGIN_PAGE_NAME = 'login'
  // set page title
  document.title = getPageTitle(to.meta.title)
  // determine whether the user has logged in
  const hasToken = getToken()
  if (!hasToken && to.name !== LOGIN_PAGE_NAME) {
    // 未登录且要跳转的页面不是登录页
    next({
      name: LOGIN_PAGE_NAME // 跳转到登录页
    })
    NProgress.done()
  } else if (!hasToken && to.name === LOGIN_PAGE_NAME) {
    // 未登陆且要跳转的页面是登录页
    next() // 跳转
    NProgress.done()
  } else if (hasToken && to.name === LOGIN_PAGE_NAME) {
    await store.dispatch('user/resetToken')
    // 已登录且要跳转的页面是登录页
    next({
      name: homeName
    })
    NProgress.done()
  } else {
    const tree = sessionStorage.getItem('tree')
    if (tree) {
      if (to.path === '/') {
        next({
          name: homeName
        })
      } else {
        next() // 跳转
      }
    } else {
      next({
        name: LOGIN_PAGE_NAME // 跳转到登录页
      })
    }

    NProgress.done()
  }
  // if (hasToken) {
  //   if (to.path === '/login') {
  //     // if is logged in, redirect to the home page
  //     next({
  //       path: '/login'
  //     })
  //     NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
  //   } else {
  //     // determine whether the user has obtained his permission roles through getInfo
  //     const menuList = store.getters.menuList && store.getters.menuList.length > 0;
  //     console.log(menuList);
  //     if (menuList) {
  //       next()
  //     } else {
  //       try {
  //         // get user info
  //         // note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
  //         // const { roles } = await store.dispatch('user/getInfo')
  //
  //         // generate accessible routes map based on roles
  //        await store.dispatch('permission/getMenu', hasToken)
  //
  //         // dynamically add accessible routes
  //         // router.addRoutes(accessRoutes)
  //
  //         // hack method to ensure that addRoutes is complete
  //         // set the replace: true, so the navigation will not leave a history record
  //         next({
  //           path: '/home'
  //         })
  //         NProgress.done()
  //       } catch (error) {
  //       //   // remove token and go to login page to re-login
  //         await store.dispatch('user/resetToken')
  //         Message.error(error || 'Has Error')
  //         next(`/login?redirect=${to.name}`)
  //         NProgress.done()
  //       }
  //     }
  //   }
  // } else {
  /* has no token*/

  // if (whiteList.indexOf(to.path) !== -1) {
  // in the free login whitelist, go directly
  //   next()
  // } else {
  // other pages that do not have permission to access are redirected to the login page.
  //     next(`/login?redirect=${to.path}`)
  //     NProgress.done()
  //   }
  // }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
